package com.fanghuaiming.ml.pay.service.impl;

import com.fanghuaiming.ml.common.model.enumeration.PayTransactionEnum;
import com.fanghuaiming.ml.pay.controller.dto.MlPayMsgDTO;
import com.fanghuaiming.ml.pay.dao.mapper.MlPayAccountMapper;
import com.fanghuaiming.ml.pay.dao.mapper.MlPayMsgMapper;
import com.fanghuaiming.ml.pay.dao.model.MlPayAccount;
import com.fanghuaiming.ml.pay.dao.model.MlPayMsg;
import com.fanghuaiming.ml.pay.service.MlPayMsgService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/****
 * @description: 支付服务接口实现类
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2020/11/27 下午1:59
 *
 */
@Service
@Slf4j
public class MlPayMsgServiceImpl implements MlPayMsgService {

    /**
     * 支付信息mapper
     */
    @Autowired
    private MlPayMsgMapper mlPayMsgMapper;

    /**
     * 支付账户mapper
     */
    @Autowired
    private MlPayAccountMapper mlPayAccountMapper;

    /**
     * @Description: 支付业务
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/4 上午11:00
     * @version:1.0.0
     */
    @Override
    @Transactional(transactionManager = "messageListPayTransactionManager")
    public PayTransactionEnum pay(MlPayMsgDTO mlPayMsgDTO){
        log.info("====== 支付微服务即将开始进行支付 支付信息:{} ======",mlPayMsgDTO);
        //查询支付人账户余额
        MlPayAccount mlPayAccount = mlPayAccountMapper.selectByUserId(mlPayMsgDTO.getUserId());
        if(null == mlPayAccount){
            log.info("====== 支付微服务 - 账户不存在 支付信息:{}======",mlPayMsgDTO);
            // 不存在该支付人
            return PayTransactionEnum.USER_IS_NOT_EXIST;
        }
        if(mlPayMsgDTO.getBalance().compareTo(mlPayAccount.getBalance()) == 1){
            //请求支付金额大与账户余额
            log.info("====== 支付微服务 - 账户余额不足,请及时充值 支付信息:{}======",mlPayMsgDTO);
            return PayTransactionEnum.DO_NOT_HAVE_ENOUGH_MONEY;
        }
        log.info("====== 支付微服务 - 扣减账户余额开始 ======");
        //更新支付账户
        mlPayAccount.setBalance(mlPayAccount.getBalance().subtract(mlPayMsgDTO.getBalance()));
        mlPayAccountMapper.updateByUserId(mlPayAccount);
        log.info("====== 支付微服务 - 扣减账户余额完成 ======");
        //新增支付信息表
        log.info("====== 支付微服务 - 更新支付信息表开始 ======");
        mlPayMsgMapper.insertPayMsg(MlPayMsg
                .builder()
                .orderId(mlPayMsgDTO.getOrderId())
                //第一次未发送
                .status(0)
                //第一次未失败
                .failCount(0)
                .createTime(new Date())
                .createUser(mlPayMsgDTO.getUserId())
                .updateTime(new Date())
                .updateUser(mlPayMsgDTO.getUserId())
                .balance(mlPayMsgDTO.getBalance())
                .build());
        //成功
        log.info("====== 支付微服务 - 更新支付信息表成功 ======");
        log.info("====== 支付微服务支付完成 ======");
        return PayTransactionEnum.SUCCESS;
    }

}
